package com.wanxi.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanxi.VO.NavigationVO;
import com.wanxi.dao.NavigationDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.entity.NavigationEntity;
import com.wanxi.service.NavigationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 19:57
 */
@Service("navigationService")
public class NavigationServiceImpl implements NavigationService {
    @Autowired
    NavigationDao navigationDao;
    @Autowired
    NavigationEntity navigationEntity;

    Jedis jedis = new Jedis();
    @Override
    public ResultDTO add(NavigationVO navigationVO) {
//        NavigationEntity navigationEntity = new NavigationEntity();
        BeanUtils.copyProperties(navigationVO,navigationEntity);
        int count = navigationDao.add(navigationEntity);
        jedis.del("navigationList");
        return ResultDTO.getResult(count);
    }
    @Override
    public ResultDTO delete(NavigationVO navigationVO) {
//        NavigationEntity navigationEntity = new NavigationEntity();
        BeanUtils.copyProperties(navigationVO,navigationEntity);
        int delete = navigationDao.delete(navigationEntity);
        jedis.del("navigationList");
        return ResultDTO.getResult(delete);
    }
    @Override
    public ResultDTO findById(NavigationVO navigationVO) {
//        NavigationEntity navigationEntity = new NavigationEntity();
        BeanUtils.copyProperties(navigationVO,navigationEntity);
        NavigationEntity byId = navigationDao.findById(navigationEntity);
        return ResultDTO.getResult(byId);
    }
    @Override
    public ResultDTO findAll(NavigationVO navigationVO) {
//        NavigationEntity navigationEntity = new NavigationEntity();
        ObjectMapper mapper = new ObjectMapper();
        BeanUtils.copyProperties(navigationVO,navigationEntity);
        int count = navigationDao.count(navigationEntity);
        List<NavigationEntity> list = new ArrayList<>();
        if (navigationEntity.getPage() !=null && navigationEntity.getPage() ==1 && jedis.exists("navigationList")){
            String jedisNavigationList = jedis.get("navigationList");
            //反序列化数组，方法2
            //factory:工厂,construct:建设，parameter:参数，parametric：参数化，type:类型
            JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, NavigationEntity.class);
            try {
                list = mapper.readValue(jedisNavigationList,javaType);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }else{
            list = navigationDao.findAll(navigationEntity);
            try {
                String navigationList = mapper.writeValueAsString(list);
                jedis.set("navigationList",navigationList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }

        return ResultDTO.getResult("", count, list);
    }
    @Override
    public ResultDTO update(NavigationVO navigationVO) {
//        NavigationEntity navigationEntity = new NavigationEntity();
        BeanUtils.copyProperties(navigationVO,navigationEntity);
        int update = navigationDao.update(navigationEntity);
        jedis.del("navigationList");
        return ResultDTO.getResult(update);
    }




}
